package cn.fastoo.task;


import cn.fastoo.common.dao.*;
import cn.fastoo.common.entity.Batch;
import cn.fastoo.common.entity.UserBalanceRemind;
import cn.fastoo.common.entity.UserBalanceRemindRecord;
import cn.fastoo.common.entity.UserBase;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.service.JdbcTemplateService;
import cn.fastoo.service.SmsrecordService;
import cn.fastoo.service.UserService;
import cn.fastoo.common.utils.Constant;
import cn.fastoo.common.utils.DateUtils;
import cn.fastoo.common.utils.SmsSender;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务发送
 *
 */
@Component
public class TimingTask {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserBaseDao userBaseDao;
    @Autowired
    private JdbcTemplateService jdbcTemplateService;
    @Autowired
    private BatchDao batchDao;
    @Autowired
    private UserService userService;
    @Autowired
    private SmsrecordDao smsrecordDao;

    @PostConstruct
    public void init() {

        ScheduledExecutorService fixedTimer = Executors.newScheduledThreadPool(1);
        fixedTimer.scheduleAtFixedRate(new Runnable() {
            public void run() {
                logger.info("timing sms send job");
                int status = 1;
                String nowTimeStr = DateUtils.formatDate(new Date(), Constant.FORMAT_YMD_HM);
                List<Long> batches = jdbcTemplateService.findFixedTimeList(status, nowTimeStr);
                logger.info("list size : " + batches.size());
                for (Long batchId : batches) {
                    (new TimingSendThread(userBaseDao,batchDao,smsrecordDao,userService,batchId)).start();
                }
            }
        }, 1, 30, TimeUnit.SECONDS); //每30秒调用一次
    }


}
